package cn.ruituo.client;

import cn.ruituo.common.Request;
import cn.ruituo.common.Response;
import cn.ruituo.common.User;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MyMainClient {
    static Socket s;
    static ObjectOutputStream oout;
    static ObjectInputStream oin;

    public static Response send(Request r) {
        try {
            oout.writeObject(r);
            oout.reset();
            Response response = (Response) oin.readObject();
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(false, "网络异常:" + e);
        }

    }

    public static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户和密码：");
        String name = sc.next();
        String pass = sc.next();
        Request r = new Request("login");
        r.put("userName", name);
        r.put("userPass", pass);
        //>>>
        Response response = send(r);
        if (response.isSuccess()) {
            User user = (User) response.get("loginUser");
            System.out.println("原来你是：" + user.getRole());
        } else {
            System.out.println("登录失败！" + response.getMsg());
        }
        // System.out.println("结果是："+response.isSuccess()+",信息是："+response.getMsg());

    }

    public static void upload() {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入上传的路径：");
        String fileName = sc.next();
        Request request = new Request("upload");
        File file = new File(fileName);
        Response response = null;
        try {
            FileInputStream bin = new FileInputStream(file);
            byte[] bb = new byte[bin.available()];
            bin.read(bb);
            bin.close();
            request.put("novelname", file.getName());
            request.put("content", bb);

            response = send(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("结果是：" + response.isSuccess() + ",信息是：" + response.getMsg());

    }

    private static void read() {
        Scanner sc = new Scanner(System.in);

        int choice = 0;
        while (true) {
            System.out.println("1:阅读，2 上传，3返回上级");
            choice = sc.nextInt();
            if (choice == 3) {
                break;
            } else if (choice == 1) {
                System.out.println("开始阅读了。。。。");
                System.out.println("天下大势，合久必分，分久必合。。。");
            } else if (choice == 2) {
                System.out.println("请输入路径：");
                String path = sc.next();
                System.out.println("上传成功了。。。");
            }

        }
    }

    public static void main(String[] args) {
        s = null;
        try {
            s = new Socket("localhost", 8888);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            oout = new ObjectOutputStream(out);
            oin = new ObjectInputStream(in);

            int choice = 0;
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("1:login,2:register,3:download，4：上传,5:阅读小说,9 退出");
                choice = sc.nextInt();
                if (choice == 1) {
                    login();
                } else if (choice == 4) {
                    upload();
                } else if (choice == 5) {
                    read();
                } else if (choice == 9) {
                    Request request = new Request("quit");
                    oout.writeObject(request);
                    oout.reset();
                    Response response = (Response) oin.readObject();
                    //if(response.isSuccess())
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s + "被关闭了。。。");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


}
